package root.action;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import root.Interceptor.UserInterceptor;
import root.service.UserService;
import root.utils.StringHelper;

import java.util.Date;

/**
 * 用户管理
 *
 * @author 吕梁山
 * @date 2018/9/21
 */
@Before(UserInterceptor.class)
public class UserAction extends BaseAction {

    private UserService userservice = new UserService();

    public void index() {
        JSONObject jsonObject = new JSONObject();
        String user_email = getPara("user_email");
        String user_name = getPara("user_name");
        int pageNumber = getParaToInt("curPage", DEFAULT_PAGENUMBER);
        Page<Record> userPage = userservice.getUserPage(user_email, pageNumber, PAGE_SIZE_15);
        jsonObject.put("code", 0);
        jsonObject.put("page", userPage);
        renderJson(jsonObject);
    }

    public void changeIsFriend(){
        JSONObject jsonObject = new JSONObject();
        String is_friend = getPara("is_friend");
        String user_id = getPara("user_id");
        Record record = new Record();
        record.set("id", user_id);
        record.set("is_friend", is_friend);
        boolean b = userservice.saveUser(record);
        if(b){
            jsonObject.put("code", 0);
            jsonObject.put("msg", "操作成功");
        } else {
            jsonObject.put("code", 1);
            jsonObject.put("msg", "操作失败");
        }
        renderJson(jsonObject);
    }
}
